<?php

namespace app\admin\controller;

use think\facade\Request;

class Home extends Base
{
    public function index(Request $request)
    {
        return view();
    }

    public function loginout()
    {
        session(null);
        if (session('?username')) {
            return $this->error('退出失败！');
        }else {
            return $this->success('退出成功！', 'admin/index/index');
        }
    }

    public function cateList()
    {
        $cateResult = model('Cate')->paginate(8);
        $viewData = [
            'cateResult' => $cateResult
        ];
        $this->assign($viewData);
        return view('catelist');
    }

    public function cateEdit()
    {
        if (request()->isPost()) {
            $data = input();
            $result = model('Cate')->edit($data);
            if ($result == 1) {
                return $this->success('修改栏目成功！', 'admin/home/catelist');
            }else {
                return $this->error($result);
            }
        }
        $cateId = input('id');
        $cateInfo = model('Cate')->where('id', '=', $cateId)->find();
        $viewData = [
            'cateInfo' => $cateInfo
        ];
        $this->assign($viewData);
        return view('cateedit');
    }

    public function cateAdd()
    {
        if (request()->isPost()) {
            $data = input();
            $result = model('Cate')->add($data);
            if ($result == 1) {
                return $this->success('栏目添加成功！', 'admin/home/catelist');
            }else {
                return $this->error($result);
            }
        }
        return view('cateadd');
    }

    public function cateDel()
    {
        $id = input('id');
        $result = model('Cate')->where('id', '=', $id)->delete();
        if ($result) {
            return $this->success('删除栏目成功！');
        }else {
            return $this->error('删除栏目失败！');
        }
    }

    public function articleList()
    {
        $articleInfo = model('Article')->paginate(8);
        $viewData = [
            'articleInfo' => $articleInfo
        ];
        $this->assign($viewData);
        return view('articlelist');
    }

    public function articleAdd()
    {
        if (request()->isPost()) {
            $data = input();
            $result = model('Article')->add($data);
            if ($result == 1) {
                return $this->success('文章添加成功！', 'admin/home/articlelist');
            }else {
                return $this->error($result);
            }
        }
        $cateInfo = model('Cate')->select();
        $viewData = [
            'cateInfo' => $cateInfo
        ];
        $this->assign($viewData);
        return view('articleadd');
    }

    public function articleDel()
    {
        $id = input('id');
        $result = model('Article')->where('id', '=' , $id)->delete();
        if ($result) {
            return $this->success('删除文章成功！');
        }else {
            return $this->error('删除文章失败！');
        }
    }

    public function articleEdit()
    {
        if (request()->isPost()) {
            $data = input('');
            $result = model('Article')->edit($data);
            if ($result == 1) {
                return $this->success('文章编辑成功！', 'admin/home/articlelist');
            }elseif ($result == 2) {
                return $this->success('文章没有发生变化！', 'admin/home/articlelist');
            }else {
                return $this->error($result);
            }
        }
        $id = input('id');
        $articleInfo = model('Article')->where('id', '=', $id)->find();
        $cateInfo = model('Cate')->select();
        $viewData = [
            'articleInfo' => $articleInfo,
            'cateInfo' => $cateInfo
        ];
        $this->assign($viewData);
        return view('articleedit');
    }

    public function userList() {
        $userInfo = model('User')->paginate(8);
        $viewData = [
            'userInfo' => $userInfo
        ];
        $this->assign($viewData);
        return view('userlist');
    }

    public function userEdit()
    {
        if (request()->isPost()) {
            $data = input();
            $result = model('User')->edit($data);
            if ($result == 1) {
                return $this->success('密码修改成功！', 'admin/home/userlist');
            }else {
                return $this->error($result);
            }
        }
        $id = input('id');
        $userInfo = model('User')->where('id', '=', $id)->find();
        $viewData = [
            'userInfo' => $userInfo
        ];
        $this->assign($viewData);
        return view('useredit');
    }

    public function userDel()
    {
        $id = input('id');
        $result = model('User')->where('id', '=', $id)->delete();
        if ($result) {
            return $this->success('会员删除成功！');
        }else {
            return $this->error('会员删除失败！');
        }
    }

    public function userAdd()
    {
        if (request()->isPost()) {
            $data = input();
            $result = model('User')->add($data);
            if ($result == 1) {
                return $this->success('会员添加成功！', 'admin/home/userlist');
            }else {
                return $this->error($result);
            }
        }
        return view('useradd');
    }

    public function system()
    {
        if (request()->isPost()) {
            $data = input();
            $result = model('System')->edit($data);
            if ($result == 1) {
                return $this->success('系统设置成功！');
            }else {
                return $this->error($result);
            }
        }
        $webInfo = model('System')->find();
        $viewData = [
            'webInfo' => $webInfo
        ];
        $this->assign($viewData);
        return view();
    }

    public function adminEdit()
    {
        if (request()->isPost()) {
            $data = input();
            $result = model('Admin')->edit($data);
            if ($result == 1) {
                return $this->success('信息修改成功！');
            }else {
                return $this->error($result);
            }
        }
        $username = input('username');
        $adminInfo = model('Admin')->where('username', '=', $username)->find();
        $viewData = [
            'adminInfo' => $adminInfo
        ];
        $this->assign($viewData);
        return view('adminedit');
    }
}
